--
-- Author: shunguo.chen
-- Date: 2016-06-17 16:14:25
--
-- RenderBase
--

local RenderBase = class("RenderBase")

function RenderBase.createRenderClass(fileName)
    local modeName = string.getModeName(fileName)
    local ClassRef = class(modeName, RenderBase)

    function ClassRef.extend(target)
    	local t = tolua.getpeer(target)
        if not t then
            t = {}
            tolua.setpeer(target, t)
        end
        setmetatable(t, ClassRef)
        return target
    end

    function ClassRef:getRenderName()
    	return self.__cname
   	end

    return ClassRef
end

function RenderBase.createPanelClass(fileName)
    local modeName = string.getModeName(fileName)
    local ClassRef = class(modeName, RenderBase)

    function ClassRef.extend(target)
    	local t = tolua.getpeer(target)
        if not t then
            t = {}
            tolua.setpeer(target, t)
        end
        setmetatable(t, ClassRef)
        return target
    end

    function ClassRef:getPanelName()
    	return self.__cname
   	end

    return ClassRef
end

function RenderBase.createListViewCell(fileName)
    local ClassRef = RenderBase.createRenderClass(fileName)

    function ClassRef:setCellModel(cellModel)
        self._cellModel = cellModel
    end

    function ClassRef:getCellModel()
        return self._cellModel
    end

    function ClassRef:getCellModelIndex()
        if self._cellModel then
            return self._cellModel.index
        end
        return 1
    end

    function ClassRef:dispatchCellEvent(eventName, params)
        if self._cellModel then
            self._cellModel:dispatchEvent(eventName, params)
        end
    end
    
    function ClassRef:refreshUIState()
    end

    function ClassRef:onFoucsIndexChange(foucsIndex)

    end

    function ClassRef:onCellsShareDataChange(shareData)
        
    end

    return ClassRef
end


RenderBase._data = nil
RenderBase._ui = nil
RenderBase._onOperate = nil

function RenderBase.createWithData(data, node)
    local sprite = RenderBase.extend(node or ccui.Widget:create())
    sprite:init(data)
    return sprite   
end

function RenderBase.create(node)
    local sprite = RenderBase.extend(node or ccui.Widget:create())
    sprite:init()
    return sprite
end

function RenderBase:init(data)
    ccui.Helper:doLayout(self)
    self._data = data
    self._ui = display.uiDelegate(self)
    
    debug.addMonitor(self, self.__cname)
end

function RenderBase:setData(data)
    self._data = data
    self:refresh()
end
function RenderBase:getData()
    return self._data
end

function RenderBase:onOperate(cb)
    self._onOperate = cb
end

function RenderBase:refresh()

end

function RenderBase:remove()

end

-- 注册 事件监听
function RenderBase:registerViewEvent(config)
    self:___deleteViewEvent()
    local viewEventHandlers = {}
    for k ,v in pairs(config) do
        local h = EM:on(v.name, v.callback)
        table.insert(viewEventHandlers, h)
    end
    self.___viewEventHandlers = viewEventHandlers
    self:enableNodeEvents()
end

function RenderBase:___deleteViewEvent()
    if self.___viewEventHandlers then
        for k ,v in pairs(self.___viewEventHandlers) do
            EM:remove(v)
        end
        self.___viewEventHandlers = nil
    end
end

function RenderBase:onExit()
    self:___deleteViewEvent()
end

return RenderBase